package org.example.lottery_system.controller.handler;

import lombok.extern.slf4j.Slf4j;
import org.example.lottery_system.common.errorcode.GlobalErrrorCodeConstants;
import org.example.lottery_system.common.exception.ControllerException;
import org.example.lottery_system.common.exception.ServiceException;
import org.example.lottery_system.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice     // 可以捕获全局抛的异常
public class GlobalExceptionHandler {
    private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> serviceException(ServiceException e){
        //打错误日志
logger.error("serviceException:",e);
        //构造错误结果
        return CommonResult.error(GlobalErrrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),e.getMessage());
    }

    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> controllerException(ServiceException e){
        //打错误日志
        logger.error("controllerException:",e);
        //构造错误结果
        return CommonResult.error(GlobalErrrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> exception(ServiceException e){
        //打错误日志
        logger.error("服务异常,不是service和controller,别的引起的:",e);
        //构造错误结果
        return CommonResult.error(GlobalErrrorCodeConstants.INTERNAL_SERVER_ERROR);//??mesage
                //哪来的
    }
}
